<script type="text/javascript">
  RED.nodes.registerType('ads-client-write-symbol', {
    paletteLabel: 'ADS - Write Symbol',
    category: 'TwinCAT ADS',
    icon: 'font-awesome/fa-angle-double-right',
    align: 'right',
    color: '#3FADB5',
    inputs: 1,
    inputLabels: function () {
      return this.variableName === '' ? "Value (topic as variable name)" : "Value"
    },
    outputs: 1,
    outputLabels: "Data",
    defaults: { 
      name: { value: '' },
      connection: { value: null, type: "ads-client-connection" },
      variableName: { value: "", required: false },
      autoFill: { value: false }
    },
    label: function () {
      if (this.connection === null)
        return `${(this.name || `ADS - Write Symbol`)} (*Not configured*)`;

      if (this.name) {
        return this.name
      }

      return `ADS - Write Symbol (${(this.variableName === '' ? `msg.topic` : `${this.variableName}`)})`;
    },
    oneditprepare: function () {
      //Stuff that is done when properties panel is opened
    }
  })
</script>



<!-- Properties -->
<script type="text/html" data-template-name="ads-client-write-symbol">
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" placeholder="Name (optional)">
  </div>

  <div class="form-row">
      <label for="node-input-connection"><i class="fa fa-bookmark"></i> ADS connection</label>
      <input type="text" id="node-input-connection" placeholder="ADS Connection">
  </div>

  <div class="form-row">
      <label for="node-input-variableName"><i class="fa fa-tag"></i> Variable name</label>
      <input type="text" id="node-input-variableName" placeholder="Variable name">
  </div>
  <div class="form-tips"><b>Tip:</b> Leave variable name empty to use <code>msg.topic</code> input instead.</div>
  
  <div class="form-row">
    <!-- Some margin after tip -->
  </div>

  <div class="form-row">
    <input type="checkbox" id="node-input-autoFill" style="display: inline-block; width: auto; vertical-align: top;">
    <label for="node-input-autoFill" style="width: auto"> Automatically fill missing properties (<a href="https://github.com/jisotalo/ads-client#example-writing-struct-type-variable-with-autofill-parameter" target="_blank"
      style="text-decoration: underline"><code>autoFill</code></a>)</label>
  </div>
</script>




<!-- Help -->
<script type="text/html" data-help-name="ads-client-write-symbol">
<p>
  Writes given value to any kind of variable by given variable name.
</p>
<p>
  The input <code>msg.payload</code> is the value to be written, its type depends to PLC variable type.
  If input <code>msg.topic</code> is given, the topic is used as variable name if no variable name is given in properties.
</p>


<h3>Properties (settings)</h3>
  <dl class="message-properties">
    <dt>Name
      <span class="property-type">string</span>
    </dt>
    <dd> Optional node name</dd>
  </dl>
  
  <dl class="message-properties">
    <dt>ADS connection
      <span class="property-type">ads-client-connection</span>
    </dt>
    <dd> ADS client connection (target system) to use</dd>
  </dl>

  <dl class="message-properties">
    <dt>Variable name
      <span class="property-type">string</span>
    </dt>
    <dd> Variable name/path to read</dd>
  </dl>

  <dl class="message-properties">
    <dt>Automatically fill missing properties
      <span class="property-type">bool</span>
    </dt>
    <dd> If data type is structured (STRUCT, FB..), any missing properties are kept as they are.</dd>
  </dl>


<h3>Inputs</h3>
  <dl class="message-properties">
    <dt>payload
      <span class="property-type">any</span>
    </dt>
    <dd> The value to write (type depends on PLC type)</dd>
  </dl>
  <dl class="message-properties">
    <dt>topic
      <span class="property-type">undefined | string</span>
    </dt>
    <dd> If given and no variable name set in properties, this topic is used as variable name.</code></dd>
  </dl>

<h3>Outputs</h3>
  <dl class="message-properties">
    <dt>payload <span class="property-type">any</span></dt>
    <dd>The value after writing</dd>
    <dt>type <span class="property-type">object</span></dt>
    <dd>Variable data type information</dd>
    <dt>symbol <span class="property-type">object</span></dt>
    <dd>Variable symbol information</dd>
    <dt>... <span class="property-type">any</span></dt>
    <dd>All input <code>msg</code> properties are forwarded</dd>
  </dl>

<h3>Details</h3>
  <p>
  The input <code>msg.payload</code> should match the PLC variable type. For example, if PLC type is <code>STRING</code>, the payload should be value that can be handled as string.
  And if the data type is STRUCT, the payload should be a JavaScript object matching the PLC struct (JSON).
  </p>
  <p>
  If <code>autoFill</code> is set, the given JSON doesn't need to include all structured data type properties. Please see 
  <a href="https://github.com/jisotalo/ads-client#example-writing-struct-type-variable-with-autofill-parameter"
    target="_blank" style="text-decoration: underline">
    ads-client writeSymbol() autoFill example
  </a>.
  </p>
  <p>
  If the variable name is not set in properties, the input <code>msg.topic</code> is used as variable name instead.
  </p>
  <p>
  See <code>ads-client</code> readme for help. Valid variable name for TwinCAT 3 can be something like <code>GVL.VariableName</code>
  </p>


<h3>References</h3>
  <ul>
    <li><a href="https://github.com/jisotalo/ads-client#writing-any-type-plc-variable" target="_blank" style="text-decoration: underline">
      ads-client writeSymbol() readme
    </a></li>
    <li><a href="https://jisotalo.github.io/ads-client/Client.html#writeSymbol" target="_blank" style="text-decoration: underline">
      ads-client writeSymbol() documentation
    </a></li>
    <li><a href="https://github.com/jisotalo/ads-client#example-writing-struct-type-variable-with-autofill-parameter" target="_blank" style="text-decoration: underline">
      ads-client writeSymbol() autoFill example
    </a></li>
  </ul>
</script>